﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tangosol.Net;
using Coherence.Linq;
using System.Linq.Expressions;
using Coherence.Contrib;
using Tangosol.Util;

namespace CoherenceSample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnAddPerson_Click(object sender, EventArgs e)
        {
            try
            {
                INamedCache cache = CacheFactory.GetCache("dist-Person");
                for (int i = 0; i < 1000; i++)
                {
                    cache.Add(i, new Person()
                    {
                        ID = i,
                        FirstName = string.Format("First Name {0}", i),
                        LastName = string.Format("LastName {0}", i),
                        Address = string.Format("Address {0}" , Guid.NewGuid()) ,
                        Title = i % 2  == 1 ? "Mr" : "Mrs"
                    });
                }
                MessageBox.Show("Done");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnGetAll_Click(object sender, EventArgs e)
        {
            try
            {
                INamedCache cache = CacheFactory.GetCache("dist-Person");
                Object[] people = (Object[])cache.GetValues(new Tangosol.Util.Filter.AlwaysFilter());

                dataGridView1.AutoGenerateColumns = true;
                dataGridView1.DataSource = people.Cast<Person>().ToArray();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                CoherenceQuery<Person> coherenceData =
                 new CoherenceQuery<Person>(
                     new CoherenceQueryProvider(CacheFactory.GetCache("dist-Person"), 
                         new MetadataCoherenceQueryTranslator()));
                string likeClause = "%8";

                var people = from person in coherenceData
                             where
                                 //(person.ID.In(new int[] { 1, 6, 9 })
                             //    ||
                                (person.FirstName.Like("Test")
                                 || person.LastName.Like(likeClause))
                                 && person.Title == "Mrs"
                             select new { person.Title, person.ID, person.LastName };

                IFilter filter = ((ICoherenceQueryable)people).Filter;

                //(person.ID ==1 || person.ID < 2) && person.LastName == "Last"  && !(person.ID==3) select person;
                //Person[] p = people.ToArray();
                dataGridView1.DataSource = people.ToArray();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void cmdDynamicQuery_Click(object sender, EventArgs e)
        {
            try
            {
                // Simple Coherence Linq Provider
                //CoherenceQuery<Person> coherenceData =
                // new CoherenceQuery<Person>(
                //     new CoherenceQueryProvider(CacheFactory.GetCache("dist-Person")));

                // MetaData Coherence Linq provider
                CoherenceQuery<Person> coherenceData =
                 new CoherenceQuery<Person>(
                     new CoherenceQueryProvider(CacheFactory.GetCache("dist-Person"),
                         new MetadataCoherenceQueryTranslator()));
                
                string likeClause = "%8";

                var people = coherenceData.Where(person=>person.ID.In(new int[]{1,6,9}));
                people = people.Where(person => person.ID.In(new int[] { 1, 6 }));
                //people = people.WhereOr(person => person.ID == 2);
                var people2 = people.Select(person=> new {person.ID, person.LastName});

                dataGridView1.DataSource = people2.ToArray();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}